package com.ybzl.gateway.utils;


import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.write.Point;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
//@Component
public class InfluxClient {


    private static String url;

    private static String token;

    private static String orgName;

    private static String bucket = "location";

    @Value("${influx.url}")
    private void setUrl(String url) {
        InfluxClient.url = url;
    }

    @Value("${influx.token}")
    private void setToken(String token) {
        InfluxClient.token = token;
    }

    @Value("${influx.org}")
    private void setOrgName(String orgName) {
        InfluxClient.orgName = orgName;
    }

    @Value("${influx.bucket}")
    private void setBucket(String bucket) {
        InfluxClient.bucket = bucket;
    }

    public static int write(String exId, String tagId,Integer rssi, Integer tagPower) {
        InfluxDBClient influxDbClient = InfluxDBClientFactory.create(url, token.toCharArray());

        // 环境监测数据
        Point p1 = Point.measurement("sc_env")
                .addField("tagId", tagId)
                .addTag("exId",exId)
                .addTag("rssi",String.valueOf(rssi))
                .addTag("tagPower",String.valueOf(tagPower));
        influxDbClient.getWriteApiBlocking().writePoint(bucket, orgName, p1);

        return 1;
    }
}
